<?php
class I18n {
    private static $initialized = false;

    private static function init() {
        if (!self::$initialized) {
            self::$initialized = true;
            $language_file = DIR_LANGUAGE . Registry::get('language')  . '/core.php';
            if (file_exists($language_file)) {
                require_once($language_file);
            }
        }
    }

    public static function reset() {
        self::$initialized = false;
    }

    public static function t($message, $params = array(), $plural = false) {
        self::init();

        $dictionary = Registry::get('dictionary');

        if (isset($dictionary[$message])) {
            $message = $dictionary[$message];
        }

        if (strstr($message, '|')) {
            $m = explode('|', $message);
            if ($plural === true || is_numeric($plural) && $plural > 1) {
                $message = $m[1];
            } else {
                $message = $m[0];
            }
        }

        if (strstr($message, '{') && strstr($message, '}') && is_array($params) && !empty($params)) {
            foreach($params as $key => $param) {
                $message = str_replace('{'.$key.'}', $param, $message);
            }
            return $message;
        }
        if (strstr($message, '%')) {
            return vsprintf($message, $params);
        } else {
            return $message;
        }
    }
}
?>